<?php
include('../pages/config.php');
session_start();
###---  This file is used for updating subscriber features

$current_user = $_SESSION['myusername'];
$directoryNumber = $_GET['queryString'];
$debug = $_GET['debug'];
$cmd = $_GET['cmd'];
$callwaitingwithcallerID = $_GET['callWaitingWithCallerID'];


$client = new SoapClient('sdk/xml/soap/ShServiceTyped.wsdl');

//  This section creates the parameters to pass to the Meta for a Pull (Query)
$params = array(
		'UserIdentity' => $directoryNumber,
		'DataReference' => 0,
		'ServiceIndication' => 'Meta_Subscriber_CallWaitingWithCallerID',
		'OriginHost' => 'amherst.com?clientVersion=7.0'
		);

//  Send the Pull (Query) to the Meta and put the returned results into $resultCode
$pullResultCode = $client->ShPull($params);
	
if ($debug == "True") {
	echo "Pre-changes results<br/>";
	echo "<pre>";
	print_r($pullResultCode);
	echo "</pre>";
}

//  If the result returns a failure, echo the result detail to the screen and stop
if ($pullResultCode->{'ResultCode'} == "5001") {
	echo $pullResultCode->{'ExtendedResult'}->{'ExtendedResultDetail'};
}
else {
	//  $Meta_Sub_Base is put in place, so we don't have to keep repeating this for each of the results below
	$Meta_Sub_Base = $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_CallWaitingWithCallerID'};
	
	//  The sequence number is a random number returned by the Meta so that multiple people can't make concurrent changes to a customer
	//  We later need to increase the number by 1 for the update
	$sequenceNumber = $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'SequenceNumber'};
	
	//  Below updates the values in the returned results according to the changes done	
	//  Sequence Number needs to be increased by 1 when it is returned.  This is per Meta's documentation
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'SequenceNumber'} = $sequenceNumber + 1;

	//  Below we will figure out what changes are being done so we can log them in the database
	//  This section needs to be before we make the changes to the results!
	$meta_changes_log = NULL;
	if ($Meta_Sub_Base->{'Subscribed'}->{'Value'} != $callwaitingwithcallerID) {
		//Caller Name has been changed
		$meta_changes_log['callwaitingwithcallerID'] = $callwaitingwithcallerID;
	}
	
	//  Below are each of the items in the returned Results that we possibly would want to change.
	//  We will "Update" the entire subscriber, even if only 1 change is made.  This is done so that multiple
	//  changes can be made and submitteid.  May look at revising this in the future.
	$Meta_Sub_Base->{'Subscribed'}->{'Value'} = $callwaitingwithcallerID;
	
	//  Need to set the UseDefault to false so that we can actually change the value above.  This has a higher
	//  presedence in deciding if a value can be changed.
	$Meta_Sub_Base->{'Subscribed'}->{'UseDefault'} = 'False';

	if ($debug == "True") {
		echo "Changes to be sent<br/>";
		echo "<pre>";
		print_r($pullResultCode->{'UserData'});
		echo "</pre>";
	}

	// This section creates the parameters to pass to the Meta for an Update
	$update_params = array(
				'UserIdentity' => $directoryNumber,
				'DataReference' => 0,
				//  $resultCode->{'UserData'} needs to be passed, because Meta wants to see the Sh-Data first
				'UserData' => $pullResultCode->{'UserData'},
				'OriginHost' => 'amherst.com?clientVersion=7.0'
				);

	//  Send the update and put the returned results into $updateResults
	$updateResults = $client->ShUpdate($update_params);	
	
	//  If the returned result code is not a 2001 (Success), then output the error returned
	if ($updateResults->{'ResultCode'} != 2001) {
		echo "<br/><br/><b>" . $updateResults->{'ExtendedResult'}->{'ExtendedResultDetail'} . "</b>";
	}
	else {
		echo "<br/><br/><b>Success!!</b><br/>";
		//  Since the result came back successful, add a log to the database if there were changes made
		//  If a form is just submitted without changes, then don't log anything because no changes were made
		if (!empty($meta_changes_log)) {
			$meta_changes_csv = NULL;
			// Parse out the array into a CSV format to be put in the database for records
			while ($item = current($meta_changes_log)) {
				$meta_changes_csv .= key($meta_changes_log) . "=" . $item . ",\n";
				next($meta_changes_log);
			}
			mysql_query("INSERT INTO metaswitch_log (trans_id, trans_dn, trans_changes, submitted_by) VALUES (NULL, '$directoryNumber', '$meta_changes_csv', '$current_user')");
		}
	}

	if ($debug == "True") {
		echo "<pre>";
		print_r($updateResults);
		echo "</pre>";
	}
}

?>

